package com.panshi.modules.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panshi.modules.domain.SysRole;
import com.panshi.modules.service.SysRoleService;
import com.panshi.vo.R;
import com.panshi.vo.SelectVO;
import com.panshi.vo.SysRoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;
import java.util.List;

/**
 * <p>
 *  前端控制器 :系统角色
 * </p>
 *
 * @author 857
 * @since 2020-07-29
 */
@Api(tags = "系统角色")
@CrossOrigin
@RestController
@RequestMapping("/api/sys/role")
public class SysRoleController {


    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/list")
    @ApiOperation("查询角色集合")
    public List<SysRoleVO> listSysRole(){
        List<SysRoleVO> sysRoles = sysRoleService.listSysRole();
        return sysRoles;
    }

    @ApiOperation("角色集合分页查询")
    @GetMapping("/pageRoleList/{page}/{limit}")
    public R pageRoleList( @PathVariable("page") Integer page,
                           @PathVariable("limit") Integer limit){
        Page<SysRole> pages = new Page<>(page,limit);
        IPage<SysRole> sysRoleIPage = sysRoleService.pageRoleList(pages);
        return R.ok().data("rows",sysRoleIPage.getRecords()).data("total",sysRoleIPage.getTotal());
    }

    @GetMapping("/{id}")
    @ApiOperation("ID获取角色信息")
    public SysRoleVO getSysRole(@PathVariable("id") Integer id) {
        SysRoleVO sysRole = sysRoleService.getSysRole(id);
        return sysRole;
    }

    @PostMapping
    @ApiOperation("新增角色")
    public R addSysRole(@RequestBody @ApiParam(name = "SysRoleVO" ,value = "新增用户参数") SysRoleVO sysRoleVO) {
        if(StringUtils.isNotBlank(sysRoleVO.getRolename())){
            if(sysRoleService.countRolename(sysRoleVO.getRolename(),0) > 0){
                return R.error().msg("用户名已存在");
            }
        }else {
            return R.error().msg("角色名称不能为空~");
        }
        int result = sysRoleService.addSysRole(sysRoleVO);
        if(result > 0){
            return R.ok();
        }
        return R.error();
    }

    @PutMapping
    @ApiOperation("编辑角色")
    public R editSysRole(@RequestBody @ApiParam(name = "SysRoleVO" ,value = "编辑用户参数") SysRoleVO sysRoleVO) {
        if(StringUtils.isNotBlank(sysRoleVO.getRolename())){
            if(sysRoleService.countRolename(sysRoleVO.getRolename(),sysRoleVO.getId()) > 0){
                return R.error().msg("用户名已存在");
            }
        }
        int result = sysRoleService.editSysRole(sysRoleVO);
        if(result > 0){
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("通过ID删除角色")
    public R delSysRole(@PathVariable("id") Integer id) {
        int result = sysRoleService.delSysRole(id);
        if(result > 0){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("查询所有角色的列表")
    @GetMapping("/selectRoleList")
    public R selectRoleList(){
        List<SelectVO> selectVOS = sysRoleService.selectRoleList();
        return R.ok().data("data",selectVOS);
    }

    @ApiOperation("通过角色ID查询对应的角色列表")
    @GetMapping("/getRoleSelectByUserId/{id}")
    public R getRoleSelectByUserId(@PathVariable("id") Integer id){
        List<SelectVO> list =sysRoleService.getRoleSelectByUserId(id);
        return R.ok().data("data",list);
    }

}

